package tt.dz.mvc.filter.reqwapper;

import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
 * 此类用于改变请求参数,覆盖父类的方法
 * 添加参数的方法.]
 * 此类还可以做SQL注入参数剔除，Html注入的相关的剔除。
 * @author huanghao
 *
 */
public class ParamterRequestWrapper extends HttpServletRequestWrapper 
{

	public ParamterRequestWrapper(HttpServletRequest request) 
	{
		super(request);
		this.newParams = new ConcurrentHashMap<String, String[]>();
	}
	
	public ParamterRequestWrapper(HttpServletRequest request,Map<String, String[]> paramters) 
	{
		super(request);
		this.newParams = new ConcurrentHashMap<String, String[]>();
		newParams.putAll(paramters);
	}
	
	private Map<String,String[]> newParams;
	

	public Map<String, String[]> getNewParams() {
		return newParams;
	}

	public void setNewParams(Map<String, String[]> newParams) {
		this.newParams = newParams;
	}

	@Override
	public String getParameter(String name) {
		 Object v = newParams.get(name);  
		  if(v==null){  
		    return null;  
		  }else if(v instanceof String[]){            
		    String []strArr=(String[]) v;  
		    if(strArr.length>0){  
		      return strArr[0];  
		    }else{  
		      return null;  
		    }  
		  }else if(v instanceof String){  
		    return (String) v;  
		  }else{  
		    return v.toString();  
		  }  
	}

	@Override
	public Map<String, String[]> getParameterMap() 
	{
		return super.getParameterMap();
	}

	@Override
	public Enumeration<String> getParameterNames() 
	{
		Vector<String> l=new Vector<String>(newParams.keySet());  
		  return l.elements();  
	}

	@Override
	public String[] getParameterValues(String name) 
	{
		Object v = newParams.get(name);  
		  if(v==null){  
		    return null;  
		  }else if(v instanceof String[]){  
		    return (String[]) v;  
		  }else if(v instanceof String){  
		    return new String[]{(String) v};  
		  }else{  
		    return new String[]{v.toString()};  
		  }  
	}
	
	public void setParamter(String key,String value)
	{
		if (newParams == null)
		{
			newParams = new ConcurrentHashMap<String, String[]>();
		}
		
		newParams.put(key, new String[]{value});
	}
	
	public void setParamter(String key,String[] value)
	{
		if (newParams == null)
		{
			newParams = new ConcurrentHashMap<String, String[]>();
		}
		
		newParams.put(key, value);
	}

}
